package Dao;

import Model.originalData;
import Utils.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class OriginalDataDao
{
    //添加
    public int add(Connection conn, originalData originalData) throws Exception{
        //添加
        String sql="Insert into originalData(" +
                "RAW_EN_TITLE,RAW_CN_TITLE,RAW_UUID,RAW_VERSION,RAW_KEYWORDS,RAW_ABSTRACT,RAW_CONTENT,RAW_TYPE," +
                "RAW_AUTHORS,RAW_AFFILIATION,RAW_TAGS,RAW_URL,RAW_MEMO,RAW_CATALOG) " +
                "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement pstm=conn.prepareStatement(sql);
        pstm.setString(1,originalData.getEgTitle());
        pstm.setString(2,originalData.getCnTitle());
        pstm.setString(3,originalData.getUID());
        pstm.setString(4,originalData.getVersion());
        pstm.setString(5,originalData.getKeyWord());
        pstm.setString(6,originalData.getAbstract());
        pstm.setString(7,originalData.getContent());
        pstm.setString(8,originalData.getSort());
        pstm.setString(9,originalData.getAuthor());
        pstm.setString(10,originalData.getCompany());
        pstm.setString(11,originalData.getLabel());
        pstm.setString(12,originalData.getLocation());
        pstm.setString(13,originalData.getMemo());
        pstm.setInt(14,originalData.getCatalog());
        return pstm.executeUpdate();
    }

    //查询
    public ResultSet list(Connection conn, originalData originalData) throws Exception{
        StringBuilder sb=new StringBuilder("select * from originalData ");
        //获取数据
        if(!StringUtils.isEmpty(originalData.getCnTitle())){
            sb.append("where RAW_CN_TITLE like '%").append(originalData.getCnTitle()).append("%'");
        }
        //执行
        String sql=sb.toString();
        PreparedStatement pstm=conn.prepareStatement(sql);
        return pstm.executeQuery();
    }

    //删除
    public int delete(Connection conn,String strid) throws Exception{
        String sql="delete from originalData where RAW_INTERNAL_ID=?";
        int id=Integer.parseInt(strid);
        PreparedStatement pstm=conn.prepareStatement(sql);
        pstm.setInt(1,id);
        return pstm.executeUpdate();
    }

    //更新修改
    public int update(Connection conn, originalData originalData) throws Exception{
        String sql="update originalData set " +
                "RAW_EN_TITLE=?,RAW_CN_TITLE=?,RAW_UUID=?,RAW_VERSION=?,RAW_KEYWORDS=?,RAW_ABSTRACT=?," +
                "RAW_CONTENT=?,RAW_TYPE=?,RAW_AUTHORS=?,RAW_AFFILIATION=?,RAW_TAGS=?,RAW_URL=? ," +
                "RAW_MEMO=?,RAW_CATALOG=? where RAW_INTERNAL_ID=?";
        PreparedStatement pstm=conn.prepareStatement(sql);
        pstm.setString(1,originalData.getEgTitle());
        pstm.setString(2,originalData.getCnTitle());
        pstm.setString(3,originalData.getUID());
        pstm.setString(4,originalData.getVersion());
        pstm.setString(5,originalData.getKeyWord());
        pstm.setString(6,originalData.getAbstract());
        pstm.setString(7,originalData.getContent());
        pstm.setString(8,originalData.getSort());
        pstm.setString(9,originalData.getAuthor());
        pstm.setString(10,originalData.getCompany());
        pstm.setString(11,originalData.getLabel());
        pstm.setString(12,originalData.getLocation());
        pstm.setString(13,originalData.getMemo());
        pstm.setInt(14,originalData.getCatalog());
        pstm.setInt(15,originalData.getID());
        return pstm.executeUpdate();
    }
}
